package com.xywl.escort.view.links

import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.LinearLayout
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.just.agentweb.AgentWeb
import com.just.agentweb.DefaultWebClient
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_service_agreement.*
import timber.log.Timber

@Route(path = "/xy/web/links")
class WebLinksActivity : BaseActivity() {

    @Autowired
    @JvmField
    var title: String = ""

    @Autowired
    @JvmField
    var url: String = ""
    private lateinit var mAgentWeb: AgentWeb
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_links)
        ARouter.getInstance().inject(this)
        showBackButton()

        if (!TextUtils.isEmpty(title)) {
            setTitleText(title)
        } else {
            setTitleText(R.string.app_name)
        }

        if (!TextUtils.isEmpty(url)) {
            mAgentWeb = AgentWeb.with(this)
                    .setAgentWebParent(layout_webView, LinearLayout.LayoutParams(-1, -1))
                    .useDefaultIndicator()
                    .setWebChromeClient(mWebChromeClient)
                    .setWebViewClient(mWebViewClient)
                    .setMainFrameErrorView(R.layout.agentweb_error_page, -1)
                    .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                    .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)//打开其他应用时，弹窗咨询用户是否前往其他应用
                    .interceptUnkownUrl() //拦截找不到相关页面的Scheme
                    .createAgentWeb()
                    .ready()
                    .go(url)
        } else {
            ToastUtil.showTip(this@WebLinksActivity, "url地址为空")
            finish()
        }
    }

    private val mWebViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
            return super.shouldOverrideUrlLoading(view, request)
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            //do you  work
            Timber.tag("SubscriptionOrderDetailActivity").e("BaseWebActivity onPageStarted")
        }
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
        }

        override fun onReceivedTitle(view: WebView, title: String) {
            super.onReceivedTitle(view, title)
        }
    }
}
